package com.project.genesis.admin.config;

import org.springframework.amqp.core.Binding;
import org.springframework.amqp.core.BindingBuilder;
import org.springframework.amqp.core.DirectExchange;
import org.springframework.amqp.core.Queue;
import org.springframework.beans.factory.annotation.Qualifier;
import org.springframework.context.annotation.Bean;
import org.springframework.context.annotation.Configuration;

import static com.project.genesis.common.constant.RabbitMQEnum.*;

/**
 * 该队列用于推送表单推送记录（push_record表）
 */
@Configuration
public class FormPushQueueConfig {
//    @Bean
//    public Queue delayedQueue() {
//        return new Queue(FORM_DELAYED_QUEUE);
//    }

    /**
     * 首次推送队列
     * @return
     */
    @Bean
    public Queue firstQueue(){
        return new Queue(FORM_FIRST_QUEUE);
    }

    /**
     * 循环推送队列
     * @return
     */
    @Bean
    public Queue afterQueue(){
        return new Queue(FORM_AFTER_QUEUE);
    }


//    // 自定义表单推送交换机
//    @Bean
//    public CustomExchange delayedExchange() {
//        Map<String, Object> args = new HashMap<>();
//        //自定义交换机的类型
//        args.put("x-delayed-type", "direct");
//        return new CustomExchange(FORM_DELAYED_EXCHANGE, "x-delayed-message", true, false, args);
//    }

    /**
     * 定义表单首次推送交换机
     * @return
     */
    @Bean
    public DirectExchange firstExchange(){
        return new DirectExchange(FORM_FIRST_EXCHANGE);
    }

    /**
     * 定义表单后续推送交换机
     */
    @Bean
    public DirectExchange afterExchange(){
        return new DirectExchange(FORM_AFTER_EXCHANGE);
    }

//    /**
//     * 绑定延迟队列
//     * @param queue
//     * @param delayedExchange
//     * @return
//     */
//    @Bean
//    public Binding bindingDelayedQueue(@Qualifier("delayedQueue") Queue queue,
//                                       @Qualifier("delayedExchange") CustomExchange
//                                               delayedExchange) {
//        return BindingBuilder.bind(queue).to(delayedExchange).with(FORM_DELAYED_ROUTING_KEY).noargs();
//    }

    /**
     * 绑定首次推送交换机和队列
     */
    @Bean
    public Binding bindingFormFirstQueue(@Qualifier("firstQueue") Queue firstQueue,
                                         @Qualifier("firstExchange") DirectExchange firstExchange){
        return BindingBuilder.bind(firstQueue).to(firstExchange).with(FORM_FIRST_ROUTING_KEY);
    }

    /**
     * 绑定循环推动交换机和队列
     */
    @Bean
    public Binding bindingFormAfterQueue(@Qualifier("afterQueue") Queue afterQueue,
                                         @Qualifier("afterExchange") DirectExchange afterExchange){
        return BindingBuilder.bind(afterQueue).to(afterExchange).with(FORM_AFTER_ROUTING_KEY);
    }

}
